package com.xc.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author xiongcan
 * @version 1.0
 * @date 2018/11/20 11:48 PM
 */
@Controller
@RequestMapping("/code/")
@Slf4j
public class VerifyCodeController {

    @Autowired
    private Producer captchaProducer;


    @RequestMapping("getCaptcha.do")
    public ModelAndView getCaptcha() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("timestamp", System.currentTimeMillis());

        mv.setViewName("test/test_code");
        return mv;
    }

    /**
     *  获取验证码
     * @return
     */
    @RequestMapping("getCode.do")
    public ModelAndView getCode(HttpServletRequest request
            , HttpServletResponse response
            , @RequestParam(value = "timestamp", required = false) String timestamp) throws IOException {

        ModelAndView mv = new ModelAndView();

        if (StringUtils.isEmpty(timestamp)) {
//            System.out.println("没有时间戳\ttimestamp:" + timestamp);
            mv.addObject("timestamp", System.currentTimeMillis());
        } else {
//            System.out.println("有时间戳\ttimestamp:" + timestamp);
            mv.addObject("timestamp", timestamp);
        }
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = captchaProducer.createText();
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
//        log.info("======生成了一个验证码，内容为：" + capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }

        return null;
    }

    /**
     *  检查验证码
     * @param timestamp
     * @param code
     * @param request
     * @return
     */
    @RequestMapping("checkCode.do")
    @ResponseBody
    public String checkCode(@RequestParam(value = "timestamp", required = false) String timestamp
            , @RequestParam(value = "code", required = false) String code
            , HttpServletRequest request) {
        boolean returnStr = false;
        String original = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        log.info("======用户输入的验证码：" + code);
        log.info("======正确的验证码：" + original);
        if (StringUtils.isNotEmpty(code)) {
            if (code.equalsIgnoreCase(original)) {
                returnStr = true;
            }
        }
        return returnStr + "";
    }




}


